package middlewares

import (
	"awesome-devops/applications/backend/modules/define"
	"gitee.com/jjawesomejj/awesomehttp/system/helper"
	"gitee.com/jjawesomejj/awesomehttp/system/httpContext"
	"gitee.com/jjawesomejj/awesomehttp/system/middlewares"
)

type GlobalMiddleware struct {
	middlewares.BaseMiddleware
}

func (baseMiddleware *GlobalMiddleware) Handle(ctx *httpContext.HttpContext, next func(ctx2 *httpContext.HttpContext) interface{}) (res interface{}) {
	nexted := next(ctx)
	switch ctx.ResponseContent.(type) {
	case error:
		content := define.BaseResponse{
			Data:  make(map[string]interface{}),
			Errno: 500,
			Msg:   ctx.ResponseContent.(error).Error(),
			Ptime: helper.NowFloat() - ctx.StartAt,
		}
		ctx.ResponseContent = content
		return nil
	default:
		ctx.ResponseContent = define.JsonResponse(*ctx, ctx.ResponseContent)
	}
	return nexted
}
